package me.xplabs.scene 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import me.xplabs.Globals;
	import me.xplabs.constant.ResType;
	import me.xplabs.constant.SceneType;
	import me.xplabs.interfaces.IScene;
	import me.xplabs.model.GameDataSingleton;
	import me.xplabs.msg.NotificationInitGameData;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.ui.baseui.Label;
	import me.xplabs.utils.Utils;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author xiaohan
	 */
	public class LoaderSceneMediator extends Mediator implements IScene
	{
		public static const NAME:String = "LoaderSceneMediator";
		private var _container:Sprite;
		private var _isInit:Boolean;
		
		private var _bgBitmap:Bitmap;
		private var _markBitmap1:Bitmap;
		private var _markBitmap2:Bitmap;
		
		private var _leftBitmap:Bitmap;
		private var _rightBitmap:Bitmap;
		private var _progressBg:Bitmap;
		private var _progressItem:Bitmap;
		private var _loadLabel:Label;
		
		public function LoaderSceneMediator( viewComponent:Object=null) 
		{
			super(NAME, viewComponent);
		}
		
		private function init():void 
		{
			if (_isInit) return;
			_isInit = true;
			_container = new Sprite();
			
			_bgBitmap = new Bitmap(new BackGround());
			
			_progressBg = new Bitmap(new ProgressBg());
			_progressItem = new Bitmap(new ProgressItem());
			
			_progressBg.x = Globals.centerW - _progressBg.width * .5;
			_progressBg.y = 495;
			_progressItem.x = Globals.centerW - _progressItem.width * .5;
			_progressItem.y = 504;
			
			_loadLabel = Globals.ui.label("加载进度：0%");
			_loadLabel.x = 300;
			_loadLabel.y = 470;
			_progressItem.scaleX = 0;
			
		}
		public function show():void 
		{
			print("显示加载视图");
			init();
			DisplayObjectContainer(viewComponent).addChild(_container);
			
			_container.addChild(_bgBitmap);
			
			_container.addChild(_progressBg);
			_container.addChild(_progressItem);
			_container.addChild(_loadLabel);
			
			
			Globals.lib.parse([Globals.domain.ui, 
							   Globals.domain.format(Globals.domain.energy + "energy", ResType.T_XML),
							   Globals.domain.format(Globals.domain.build + "build", ResType.T_XML),
							   Globals.domain.format(Globals.domain.build + "flag", ResType.T_XML),
							   Globals.domain.format(Globals.domain.map + "raidbuttons", ResType.T_XML),
							   Globals.domain.format(Globals.domain.map + "raidlock", ResType.T_XML),
							   Globals.domain.format(Globals.domain.skill + "buff", ResType.T_XML),
							   Globals.domain.format(Globals.domain.skill + "skill", ResType.T_XML)],
							  { onComplete:completeHandler , onProgress:progressHandler } );
		}
		
		private function progressHandler(e:LoaderEvent):void 
		{
			_loadLabel.text = "加载进度" + int(LoaderMax(e.currentTarget).progress * 100);
			_progressItem.scaleX = LoaderMax(e.currentTarget).progress;
			_loadLabel.x = Globals.centerW - _loadLabel.width * .5;
			print(_loadLabel.text);
		}
		
		private function completeHandler(e:LoaderEvent):void 
		{
			GameDataSingleton.i.builds = Globals.lib.getResObj(Globals.domain.format(Globals.domain.build + "build", ResType.T_XML));
			GameDataSingleton.i.skills = Globals.lib.getResObj(Globals.domain.format(Globals.domain.skill + "skill", ResType.T_XML));
			GameDataSingleton.i.buffs = Globals.lib.getResObj(Globals.domain.format(Globals.domain.skill + "buff", ResType.T_XML));
			GameDataSingleton.i.energys = Globals.lib.getResObj(Globals.domain.format(Globals.domain.energy + "energy", ResType.T_XML));
			GameDataSingleton.i.flags = Globals.lib.getResObj(Globals.domain.format(Globals.domain.build + "flag", ResType.T_XML));
			GameDataSingleton.i.setRaidButtons(Globals.lib.getResObj(Globals.domain.format(Globals.domain.map + "raidbuttons", ResType.T_XML)));
			GameDataSingleton.i.setRaidLocks(Globals.lib.getResObj(Globals.domain.format(Globals.domain.map + "raidlock", ResType.T_XML)));
			sendNotification(NotificationName.INIT_LOADER_COMPLETE, Globals.lib.getResObj(Globals.domain.format(Globals.domain.energy + "energy", ResType.T_XML)), NotificationInitGameData.INIT_ENERGY_DATA);
			sendNotification(NotificationName.INIT_LOADER_COMPLETE, Globals.lib.getResObj(Globals.domain.format(Globals.domain.build + "build", ResType.T_XML)), NotificationInitGameData.INIT_BUILD_DATA);
			sendNotification(NotificationName.INIT_LOADER_COMPLETE, Globals.lib.getResObj(Globals.domain.format(Globals.domain.build + "flag", ResType.T_XML)), NotificationInitGameData.INIT_FLAG_DATA);
			sendNotification(NotificationName.INIT_LOADER_COMPLETE, Globals.lib.getResObj(Globals.domain.format(Globals.domain.skill + "skill", ResType.T_XML)), NotificationInitGameData.INIT_SKILL_DATA);
			sendNotification(NotificationName.CHANGE_SCENE, SceneType.START_SCENE );
		}
		
		public function dispose():void 
		{
			print("销毁加载视图");
			if (DisplayObjectContainer(viewComponent).contains(_container)) 
				DisplayObjectContainer(viewComponent).removeChild(_container);
			_container.removeChild(_bgBitmap);
			
			_container.removeChild(_progressBg);
			_container.removeChild(_progressItem);
			_container.removeChild(_loadLabel);
			
			_bgBitmap.bitmapData.dispose();
			_progressBg.bitmapData.dispose();
			_progressItem.bitmapData.dispose();
			
			_bgBitmap.bitmapData 		= null;
			_progressBg.bitmapData      = null;
			_progressItem.bitmapData    = null;

		}
		
		public function get container():Sprite 
		{
			return _container;
		}
	}
}